<?php


namespace app\controller\admin;


use app\models\user\UserLevelModel;
use app\validate\UserLevelValidate;

class UserLevelController extends BaseAdminController
{

    function setSearch()
    {
        // TODO: Implement setSearch() method.
    }

    function setSort()
    {
        // TODO: Implement setSort() method.
    }

    public function getList()
    {
        $list = UserLevelModel::where('status', 0)
            ->order('id', 'desc')
            ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
            ->toArray();
        return $this->success($list);
    }

    /**
     * 添加，编辑
     * @return \think\response\Json
     */
    public function add()
    {
        try {
            $data = $this->request->param();
            validate(UserLevelValidate::class)->scene('add')->check($data);
            UserLevelModel::create($data);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function edit()
    {
        try {
            $data = $this->request->only(['id', 'name', 'amount', 'img', 'intro', 'preferential']);
            validate(UserLevelValidate::class)->scene('edit')->check($data);
            UserLevelModel::update($data);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function del()
    {
        try {
            $id = $this->request->param('id');
            $model = UserLevelModel::find($id);
            if (!$model) {
                throw new \Exception("非法请求");
            }
            $model->status = 1;
            $model->save();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}